package com.backend.modules.task.entity;

import com.backend.common.entity.BaseEntity;
import com.backend.modules.system.entity.SysUser;
import com.backend.modules.task.enums.SubmissionStatus;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 任务提交记录实体
 * 记录学生对任务的提交情况
 */
@Data
@Entity
@Table(name = "task_submission")
@EqualsAndHashCode(callSuper = true)
@ToString(exclude = {"task", "student", "files"})
@SQLDelete(sql = "UPDATE task_submission SET deleted = true WHERE id = ?")
@Where(clause = "deleted = false")
public class TaskSubmission extends BaseEntity {
    
    /**
     * 关联的任务
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "task_id")
    private Task task;
    
    /**
     * 任务ID
     */
    @Column(name = "task_id", insertable = false, updatable = false)
    private Long taskId;
    
    /**
     * 提交学生
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "student_id")
    private SysUser student;



    /**
     * 提交说明
     */
    @Column(columnDefinition = "text")
    private String comment;
    
    /**
     * 提交时间
     */
    @Column(nullable = false)
    private LocalDateTime submitTime;
    
    @Column(name = "status", nullable = false)
    private String status = SubmissionStatus.PENDING.name();
    
    @Column(name = "review_comment")
    private String reviewComment;
    
    @Column(name = "review_time")
    private LocalDateTime reviewTime;


    @OneToMany(mappedBy = "submission", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy("sort asc")
    private List<TaskSubmissionFile> files = new ArrayList<>();

    public void addFile(TaskSubmissionFile file) {
        files.add(file);
        file.setSubmission(this);
    }

    public void removeFile(TaskSubmissionFile file) {
        files.remove(file);
        file.setSubmission(null);
    }
} 